package com.guigu.xm.bjy.controller;

import com.guigu.xm.advice.Interceptor;
import com.guigu.xm.bjy.service.impl.DFileServiceImpl;
import com.guigu.xm.pojo.DFile;
import com.guigu.xm.pojo.DModule;
import com.guigu.xm.pojo.DModuleDetails;
import com.guigu.xm.pojo.PageVo;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wl")
public class DmoduleController {
    @Autowired(required = false)
    DFileServiceImpl dFileService;
    @Autowired(required = false)
    HttpServletRequest request;
    @Autowired(required = false)
    HttpServletResponse response;

    @PostMapping("/addDmodule")
    @ResponseBody
    public int tj(String procedureName,String parentId,String designer,
                  String register,String registerTime,String moduleDescribe,
                  String cpbh,Integer[] xiaoji) throws ParseException {
       int aa=0;
       for (int i=0;i<xiaoji.length;i++){
           aa+=xiaoji[i];
           System.out.println(aa);
       }
        /*设计编号*/
        String s = dFileService.selId();
        int substring = Integer.parseInt(s.substring(11, 15));
        int i = substring+ 10000 + 1;
        String ss = String.valueOf(i);
        String lsh = ss.substring(1,5);
        Date date= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String xx = sdf.format(date);
        DModule dModule=new DModule();
        dModule.setDesignId("200"+xx+""+lsh+"");
        /*结束*/
        dModule.setProductName(procedureName);
        dModule.setProductId(parentId);
        dModule.setDesigner(designer);
        dModule.setRegister(register);
        dModule.setCheckTag("S001-0");
        dModule.setChangeTag("B002-0");
        dModule.setCostPriceSum(aa);
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = dateFormat.parse(registerTime);
        dModule.setRegisterTime(parse);
        dModule.setModuleDescribe(moduleDescribe);
        dModule.setProductId(cpbh);
        int i1 = dFileService.addDModule(dModule);
        return i1;
    }
    @RequestMapping("/addDModuleDetails")
    @ResponseBody
    public int tj2(String bh,String name,String type, String shuliang,
                   String dw,String pp, String xiaoji, String cpbh){
        DModuleDetails dModuleDetails=new DModuleDetails();
        dModuleDetails.setParentId(cpbh);
        dModuleDetails.setProductId1(bh);
        dModuleDetails.setProductName1(name);
        dModuleDetails.setType(type=="物料"?"物料":"物料");
        dModuleDetails.setAmount(Integer.parseInt(shuliang));
        dModuleDetails.setAmountUnit(dw);
        dModuleDetails.setCostPrice(Integer.parseInt(pp));
        dModuleDetails.setSubtotal(Integer.parseInt(xiaoji));
        System.out.println(dModuleDetails);
        int i = dFileService.addDModuleDetails(dModuleDetails);
        return i;
    }

    /*查询所有物料审核*/
    @RequestMapping("/all")
    @ResponseBody
    public PageVo<DModule> FeList(@RequestBody Map<String,String> map){
        System.out.println(map);
        String page = map.get("page");
        String pageNum = map.get("num");
        PageVo<DModule> dModulePageVo = dFileService.find(Integer.valueOf(page), Integer.valueOf(pageNum));
        System.out.println("/"+dModulePageVo);
        return dModulePageVo;
    }

    /*查询商品编号点复核跳页面*/
    @GetMapping("/findById")
    @ResponseBody
    public void findById(){
        String id = request.getParameter("id");
        try {
            request.setAttribute("fhId",id);
            request.getRequestDispatcher("/jy/wlshd.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*查询商品编号赋值到页面中*/
    @GetMapping("/findDmoduleById")
    @ResponseBody
    public DModule findById(Integer id){
        DModule dModule = new DModule();
        dModule.setId(id);
        return dFileService.ByDmoduleId(id);


    }

    @Interceptor(methodName = "用户登录")
    /*复核修改状态*/
    @PostMapping("/upDmoduleById")
    @ResponseBody
    public int upDFileById(@RequestBody DModule dModule){
       return dFileService.upDmoduleByID(dModule);
    }

    /*查询商品编号赋值到页面中小的table中*/
    @GetMapping(value = "findDModuleDetails/{parentId}")
    @ResponseBody
    public List<DModuleDetails> all(@PathVariable("parentId") String parentId){
        DModuleDetails dModuleDetails = new DModuleDetails();
        dModuleDetails.setParentId(parentId);
        return dFileService.all(parentId);


    }
}
